import { isPlainObject } from 'lodash-es'

export function isPromise(obj: any) {
  if (!obj) return false;

  return typeof obj.then === 'function' && typeof obj.catch === 'function'
}

export function anyFuncCall(func: Function, bind: any = null, ...args) {
  return new Promise((reslove, reject) => {
    const res = func.call(bind, ...args)
    if (isPromise(res)) {
      res.then(reslove).catch(reject)
    } else {
      reslove(res)
    }
  })
}

export function boolFuncCall(func: boolean | Function, bind: any = null, ...args) {
  if (func === void 0) {
    return true;
  }
  if (typeof func === 'boolean') {
    return func;
  }
  if (typeof func === 'function') {
    return func.call(bind, ...args)
  }
}